//
//  BaseViewController.m
//  YOYOLive
//
//  Created by 王根强 on 2016/10/10.
//  Copyright © 2016年 王根强. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController (){
    UIImageView*navBarHairlineImageView;//隐藏导航栏底部黑线
}
@end

@implementation BaseViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    navBarHairlineImageView.hidden=YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    
    navBarHairlineImageView= [self findHairlineImageViewUnder:self.navigationController.navigationBar];
}

- (void)setShowNavView:(BOOL)showNavView
{
    _showNavView = showNavView;
    if (showNavView) {
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
        [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
//        backBtn.hitEdgeInsets = UIEdgeInsetsMake(-15, -15, -15, -15);
        [self.view addSubview:backBtn];
        [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(24);
            make.top.offset(64);
            make.left.offset(20);
            make.height.mas_equalTo(24);
        }];
        
        self.navTitleLab = [[UILabel alloc] init];
        self.navTitleLab.font = JSS_FONT_6;
        self.navTitleLab.textAlignment = NSTextAlignmentCenter;
        self.navTitleLab.textColor = RGB_VALUE(0x5B6067);
        [self.view addSubview:self.navTitleLab];
        [self.view bringSubviewToFront:self.navTitleLab];
        [self.navTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(200);
            make.centerX.offset(0);
            make.height.mas_equalTo(22);
            make.centerY.equalTo(backBtn);
        }];
    }
}

- (void)backBtnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)showHUD {
    _hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
}

- (void)showHUD:(NSString *)title {
    if (_hud == nil) {
        _hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    }
    _hud.label.text = title;
}

- (void)hidenHUD {
    [_hud hideAnimated:YES];
}

- (void)showToast:(NSString *)text {//只显示文本框的toast提示 持续三秒
    UIWindow * window = [[UIApplication sharedApplication]keyWindow];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = text;
    hud.offset = CGPointMake(0.f, 0.f);
    [hud hideAnimated:YES afterDelay:3.f];
}

- (void)showAlertWithTitle:(NSString *)title
                   message:(NSString *)message
               cancelTitle:(NSString *)cancelTitle {
    if (IOS_VERSION >= 9.0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:cancleAction];
    } else {//版本向下兼容
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:cancelTitle otherButtonTitles:nil, nil];
        [alert show];
    }
}

//隐藏导航栏底部黑线
- (UIImageView*)findHairlineImageViewUnder:(UIView*)view {
    if([view isKindOfClass:UIImageView.class] && view.bounds.size.height<=1.0) {
        return(UIImageView*)view;
    }
    for(UIView*subview in view.subviews) {
        UIImageView*imageView = [self findHairlineImageViewUnder:subview];
        if(imageView) {
            return imageView;
        }
    }
    return nil;
}


@end
